import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:im_app/im/page/view/TransferMessageSheetView.dart';
import 'package:im_app/im/util/UiUtil.dart';

import '../../pojo/ImPojo.dart';
import '../bloc/MessageListCubit.dart';
import 'component/UiUtil.dart';

class MessageHandlerView extends StatelessWidget {
  const MessageHandlerView({super.key});

  @override
  Widget build(BuildContext context) {
    var messageListCubit = context.read<MessageListCubit>();
    return Padding(
      padding: const EdgeInsets.only(top: 10, bottom: 10),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceAround,
        children: [
          _buildTransferPane(context, messageListCubit),
          _buildCollectPane(context, messageListCubit),
          _buildDeletePane(context, messageListCubit),
          _buildCancelPane(messageListCubit),
        ],
      ),
    );
  }

  Widget _buildTransferPane(BuildContext context, MessageListCubit messageListCubit) {
    return GestureDetector(
      onTap: () {
        var messages = messageListCubit.state.selectedMessages.map((wm) => wm.message).toList(growable: false);
        if (messages.isEmpty) {
          UiUtil.showSnackBar(context, '请选择消息');
          return;
        }
        var audioMessages = messages.where((m)=>m.messageType == ImMessageType.Audio).length;
        if (audioMessages > 0) {
          UiUtil.showSnackBar(context, '不能转发语音消息');
          return;
        }
        showBottomSheet(
            context: context,
            builder: (context) {
              return TransferMessageSheetView(messages: messages);
            });
      },
      child: buildSvgView('assets/svg/transfer2.svg', 30, 30, Colors.black),
    );
  }

  Widget _buildCollectPane(BuildContext context, MessageListCubit messageListCubit) {
    return GestureDetector(
      onTap: () {
        var messages = messageListCubit.state.selectedMessages;
        if (messages.isEmpty) {
          UiUtil.showSnackBar(context, '请选择消息');
          return;
        }
        var audioMessages = messages.where((m)=>m.message.messageType == ImMessageType.Audio).length;
        if (audioMessages > 0) {
          UiUtil.showSnackBar(context, '不能收藏语音消息');
          return;
        }
        messageListCubit.add(MessageListCollectMessagesEvent());
      },
      child: buildSvgView('assets/svg/collect.svg', 30, 30, Colors.black),
    );
  }

  Widget _buildDeletePane(BuildContext context, MessageListCubit messageListCubit) {
    return GestureDetector(
      onTap: () {
        var messages = messageListCubit.state.selectedMessages;
        if (messages.isEmpty) {
          UiUtil.showSnackBar(context, '请选择消息');
          return;
        }
        messageListCubit.add(MessageListDeleteMessagesEvent());
      },
      child: buildSvgView('assets/svg/delete.svg', 30, 30, Colors.black),
    );
  }

  Widget _buildCancelPane(MessageListCubit messageListCubit) {
    return GestureDetector(
      onTap: () {
        messageListCubit.add(MessageListSelectableEvent(false));
      },
      child: const Icon(Icons.cancel_outlined),
    );
  }
}
